﻿@model IEnumerable<Angel.Web.Models.FilterUser>      

@using Angel.Entities;
 <table width="100%" class="tbContent">
    <thead >
        <tr class="hd">
		    <td> Username </td>
			<td> Full Name </td>
            <td> Login Details </td>
            <td> Groups </td>
            <td> Directory </td>
            <td> Operations </td>
        </tr>
    </thead>
	<tbody>        
@if (Model != null)
{
    foreach (var item in Model)
    {
        <tr>
            <td class="tbusername"><a href="#">@Html.DisplayFor(model => item.UserName)</a></td>
            <td>
                <li class="tbfullname">@Html.DisplayFor(model => item.FullName)</li>
                <a href="#" class="tbmail">@Html.DisplayFor(model => item.Email)</a>
            </td>
            <td class="tblogindetails">@Html.DisplayFor(model => item.LoginDetails)</td>
            <td class="tbgroup">
                @{
                    foreach(Group g in item.Groups){
                        <a href = "#"><li style="padding-left:5px; line-height:20px; list-style:nomal;">@g.GroupName</li></a>
                    }
                    }
            </td>
            <td class="tbdirectory">@Html.DisplayFor(model => item.Directory)</td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.UserID })
                @Html.ActionLink("Delete", "Delete", new { id = item.UserID })
            </td>
        </tr>
    }
}
else
{
    <h5>Display 0 result</h5>
}
    </tbody>
</table>

                        
                    @helper buildLinks(int start, int end, string innerContent)
                        {
                           for (int i = start; i <= end; i++)
                           {
                               @Ajax.ActionLink(innerContent ?? i.ToString(),"Index_Filter",new { page = i},  new AjaxOptions { UpdateTargetId = "listUser" })
                               <tt></tt>
                               //<a class="@(i == ViewBag.currentPage ? "current" : "")" href="@Ajax.ActionLink(innerContent ?? i.ToString(), "Index_Filter","User", new { page = i }, new AjaxOptions { UpdateTargetId = "listUser" })"></a>  
                           }
     
                        }

                    @helper pageLinks()
                        {
                            const int maxPages = 5;
                            if (ViewBag.totalPage <= maxPages)
                            {
                                    @buildLinks(1, (int)ViewBag.totalPage, null)
                               return;
                            }

                            int pagesAfter = (int)(ViewBag.totalPage - ViewBag.currentPage);
                            int pagesBefore = (int)ViewBag.currentPage - 1;
                            if (pagesAfter <= 4)
                            {
                            @buildLinks(1, 1, null)
                       int pageSubset = (int)ViewBag.totalPage - maxPages - 1 > 1 ? (int)ViewBag.totalPage - maxPages - 1 : 2;
                            @buildLinks(pageSubset, pageSubset, "...")
                            @buildLinks((int)ViewBag.totalPage - maxPages + 3, (int)ViewBag.totalPage, null) 
                       return; // Exit
                            }
                            if (pagesBefore <= 4)
                            {
                            @buildLinks(1, maxPages - 2, null) 
                       int pageSubset = maxPages + 2 < (int)ViewBag.totalPage ? maxPages + 2 : (int)ViewBag.totalPage - 1;
                            @buildLinks(pageSubset, pageSubset, "...") 
                            @buildLinks((int)ViewBag.totalPage, (int)ViewBag.totalPage, null)
                       return; // Exit
                            }

                            if (pagesAfter > 4)
                            {
                            @buildLinks(1, 1, null) 
                       int pageSubset1 = (int)(ViewBag.currentPage - 7 > 1 ? ViewBag.currentPage - 7 : 2);
                       int pageSubset2 = (int)(ViewBag.currentPage + 7 < ViewBag.totalPage ? ViewBag.currentPage + 7 : ViewBag.totalPage - 1);
                            @buildLinks(pageSubset1, pageSubset1, pageSubset1 == (int)ViewBag.currentPage - 4 ? null : "...")
                            @buildLinks(ViewBag.currentPage - 3, ViewBag.currentPage + 3, null)
                            @buildLinks(pageSubset2, pageSubset2, pageSubset2 == (int)ViewBag.currentPage + 4 ? null : "...")
                            @buildLinks(ViewBag.totalPage, ViewBag.totalPage, null) 
                       return;
                            }    
                        }
                <div class="pagination">
                    Page: 
                    @pageLinks()   
                </div>